package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class BjdNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		//http://www.bjd.com.cn/jryw/201211/22/t20121122_3443081.html
		boolean bRet=false;
		String[] regex={
				"http://.*[.]bjd[.]com[.]cn/.*/[0-9]{6}/[0-9]{2}/t[0-9]{8}_[0-9]*[.]html",
				"http://www.bjd.com.cn/[a-z]*/[a-z]*/[0-9]*/[0-9]*/.*.html"
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if ( html== null) {
		}
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;;
		Long date=0l;
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title= doc.select("div.zj_text div.zj_tit").text();
		if(StringUtils.isBlank(title)){
			title= doc.select("title").text();
			if(StringUtils.isBlank(title)){
				title= doc.select("h6").text();
			}
		}

		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div.info span").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("td[width=200]").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("table.boxcenter").text());
			}
		}
		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div.zj_txt"));
		if(StringUtils.isBlank(content)){
			content= HtmlCleaner.getContentHtml(url,doc.select("table.txtcon"));
		}
		if(StringUtils.isBlank(content)){
			content= HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		String author= JSoupUtils.matchAuthor(doc, "来源：");
		meta.setAuthor(author);
		return meta;
	}

	public static void main(String[] args) {
		String url="http://www.bjd.com.cn/jx/jj/201611/08/t20161108_11043911.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		BjdNewsAnalyse cnhubeiNewsAnalyse=new BjdNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		System.out.println(detailPage);
		if(detailPage){
		NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
		System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
